﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MapEditorBlockView :BaseWidget
{
    public UIWidget BG;
    public GameObject BlockGrid;
    public GameObject SelectGrid;
    public GameObject SelectPrefab;
    public GameObject NONESelect;

    public System.Action<int> SelectIndexFn;

    public int SelectBlockIndex = -1; 
    public override void OnLoad()
    {
        BG.SetAnchor(UIRoot.list[0].gameObject);
        LoadBlocks(MapTableMgr.BlockTable);
    }

    void LoadBlocks(Hashtable blockTable)
    {
        int i = 0;
        float addStep = 110f;
        foreach(DictionaryEntry block in blockTable)
        {
            Hashtable blockTableItem = (Hashtable)block.Value;
            string prefabPath = (string)blockTableItem[":prefab-path"];
            float width = (float)blockTableItem[":width"];
            float height = (float)blockTableItem[":height"];

            float scaleSize =60f / (float)Mathf.Max(width,height);

            //Init Block
            GameObject blockItem  = (GameObject)GameObject.Instantiate(Resources.Load(prefabPath));
            blockItem.transform.parent = BlockGrid.transform;
            blockItem.transform.localScale = new Vector3(width* scaleSize, height* scaleSize, scaleSize);
            blockItem.layer =  5;
            float newPosX = i * addStep - BG.width * 0.5f + 150f;
            blockItem.transform.localPosition = new Vector3(newPosX, 0,-100)  ;
            Destroy(blockItem.GetComponent<BoxCollider>());

            //Init SelectUI
            GameObject selectUI = (GameObject)GameObject.Instantiate(SelectPrefab);
            selectUI.transform.parent = SelectGrid.transform;
            selectUI.transform.localScale = Vector3.one;
            selectUI.transform.localPosition = new Vector3(newPosX, 0,0);
            selectUI.SetActive(true);
            selectUI.name = block.Key.ToString();
            selectUI.GetComponent<UISprite>().color = new Color32(0,0,0,1);
            UIEventListener.Get(selectUI).onClick = OnClickBlock;
            i++;
        }
        NONESelect.transform.localPosition = new Vector3(-BG.width * 0.5f+50f,0,0);
        UIEventListener.Get(NONESelect).onClick = OnClickNONE;
    }

    void OnClickBlock(GameObject go)
    {
        SelectBlock(int.Parse(go.name));
    }

    void SelectBlock(int index)
    {
        if (SelectBlockIndex != -1)
        {
            SelectGrid.transform.Find(SelectBlockIndex.ToString()).GetComponent<UISprite>().color = new Color32(0, 0, 0, 1);
        }
        if (index != -1)
        {
            NONESelect.GetComponent<UISprite>().color = Color.white;
            SelectGrid.transform.Find(index.ToString()).GetComponent<UISprite>().color = Color.black;
        }
        else
        {
            NONESelect.GetComponent<UISprite>().color = Color.black;
        }
        SelectBlockIndex = index;
        if (SelectIndexFn != null)
        {
            SelectIndexFn(index);
        }
    }
   
    void OnClickNONE(GameObject go)
    {
        SelectBlock(-1);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            this.SelectBlock(-1);
        }
        for (int i=0;i<BlockGrid.transform.childCount;i++)
        {
            Transform blockItem = BlockGrid.transform.GetChild(i);
            blockItem.Rotate(new Vector3(0,1,0));
        }
    }
}
